package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.AdvancedScheduleRequestDTO;
import com.demo.pojo.entity.Clazz;
import com.demo.pojo.entity.Course;
import com.demo.service.impl.AdvancedScheduleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 高级智能排课测试控制器
 */
@RestController
@RequestMapping("/advanced-test")
@CrossOrigin
public class AdvancedScheduleTestController {
    
    @Autowired
    private AdvancedScheduleServiceImpl advancedScheduleService;
    
    /**
     * 测试1：为特定班级排课
     */
    @PostMapping("/test-specific-clazzes")
    public Result<String> testSpecificClazzes() {
        try {
            AdvancedScheduleRequestDTO request = new AdvancedScheduleRequestDTO();
            request.setSemester("2025-1");
            request.setSelectedClazzIds(Arrays.asList(1L, 2L, 3L)); // 选择前3个班级
            request.setMaxPeriodsPerWeek(10); // 每周最多10节课
            request.setMaxPeriodsPerDay(6); // 每天最多6节课
            request.setEvenDistribution(true);
            request.setAvoidTeacherConflict(true);
            request.setAvoidClassroomConflict(true);
            request.setPriority(4);
            request.setRemark("测试特定班级排课");
            
            boolean success = advancedScheduleService.executeAdvancedSchedule(request);
            
            if (success) {
                return Result.success("特定班级排课测试成功！\n" +
                    "选择班级：1, 2, 3\n" +
                    "每周最大课时：10\n" +
                    "每天最大课时：6");
            } else {
                return Result.error("特定班级排课测试失败");
            }
            
        } catch (Exception e) {
            return Result.error("测试失败：" + e.getMessage());
        }
    }
    
    /**
     * 测试2：为特定课程排课
     */
    @PostMapping("/test-specific-courses")
    public Result<String> testSpecificCourses() {
        try {
            AdvancedScheduleRequestDTO request = new AdvancedScheduleRequestDTO();
            request.setSemester("2025-1");
            request.setSelectedCourseIds(Arrays.asList(1L, 2L, 3L, 4L, 5L)); // 选择前5个课程
            request.setMaxPeriodsPerWeek(8); // 每周最多8节课
            request.setMaxPeriodsPerDay(4); // 每天最多4节课
            request.setEvenDistribution(true);
            request.setAvoidTeacherConflict(true);
            request.setAvoidClassroomConflict(true);
            request.setPriority(5);
            request.setRemark("测试特定课程排课");
            
            boolean success = advancedScheduleService.executeAdvancedSchedule(request);
            
            if (success) {
                return Result.success("特定课程排课测试成功！\n" +
                    "选择课程：1, 2, 3, 4, 5\n" +
                    "每周最大课时：8\n" +
                    "每天最大课时：4");
            } else {
                return Result.error("特定课程排课测试失败");
            }
            
        } catch (Exception e) {
            return Result.error("测试失败：" + e.getMessage());
        }
    }
    
    /**
     * 测试3：严格课时限制排课
     */
    @PostMapping("/test-strict-limits")
    public Result<String> testStrictLimits() {
        try {
            AdvancedScheduleRequestDTO request = new AdvancedScheduleRequestDTO();
            request.setSemester("2025-1");
            request.setMaxPeriodsPerWeek(6); // 每周最多6节课（严格限制）
            request.setMaxPeriodsPerDay(2); // 每天最多2节课（严格限制）
            request.setEvenDistribution(true);
            request.setAvoidTeacherConflict(true);
            request.setAvoidClassroomConflict(true);
            request.setPriority(5);
            request.setRemark("测试严格课时限制排课");
            
            boolean success = advancedScheduleService.executeAdvancedSchedule(request);
            
            if (success) {
                return Result.success("严格课时限制排课测试成功！\n" +
                    "每周最大课时：6（严格限制）\n" +
                    "每天最大课时：2（严格限制）\n" +
                    "均匀分配：是");
            } else {
                return Result.error("严格课时限制排课测试失败");
            }
            
        } catch (Exception e) {
            return Result.error("测试失败：" + e.getMessage());
        }
    }
    
    /**
     * 测试4：宽松排课（允许冲突）
     */
    @PostMapping("/test-loose-schedule")
    public Result<String> testLooseSchedule() {
        try {
            AdvancedScheduleRequestDTO request = new AdvancedScheduleRequestDTO();
            request.setSemester("2025-1");
            request.setMaxPeriodsPerWeek(20); // 每周最多20节课（宽松）
            request.setMaxPeriodsPerDay(10); // 每天最多10节课（宽松）
            request.setEvenDistribution(false); // 不要求均匀分配
            request.setAvoidTeacherConflict(false); // 允许教师冲突
            request.setAvoidClassroomConflict(false); // 允许教室冲突
            request.setPriority(1);
            request.setRemark("测试宽松排课");
            
            boolean success = advancedScheduleService.executeAdvancedSchedule(request);
            
            if (success) {
                return Result.success("宽松排课测试成功！\n" +
                    "每周最大课时：20（宽松）\n" +
                    "每天最大课时：10（宽松）\n" +
                    "均匀分配：否\n" +
                    "允许教师冲突：是\n" +
                    "允许教室冲突：是");
            } else {
                return Result.error("宽松排课测试失败");
            }
            
        } catch (Exception e) {
            return Result.error("测试失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取测试用的班级列表
     */
    @GetMapping("/test-clazzes")
    public Result<List<Clazz>> getTestClazzes() {
        try {
            List<Clazz> clazzes = advancedScheduleService.getAvailableClazzes();
            return Result.success(clazzes);
        } catch (Exception e) {
            return Result.error("获取班级列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取测试用的课程列表
     */
    @GetMapping("/test-courses")
    public Result<List<Course>> getTestCourses() {
        try {
            List<Course> courses = advancedScheduleService.getAvailableCourses();
            return Result.success(courses);
        } catch (Exception e) {
            return Result.error("获取课程列表失败：" + e.getMessage());
        }
    }
}
























